import {
    createSlice
} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";

export type TPanes = {
    title:string,
    key:string,
    closable:boolean
}
type TCommonState = {
    panes:TPanes[]
}
const initialState = {
    panes:[
        {
            title:"主页",
            key:"/",
            closable:false
        }
    ]
}
const commonSlice = createSlice({
    name:"common",
    initialState,
    reducers:{
        // 添加panes
        addPanes(state,{payload}){
            // {title,key,cloasble}
            const info:TPanes = state.panes.find((v:TPanes)=>v.key === payload.key) as TPanes;
            if(!info){
                state.panes.push(payload);
            }
        },
        // 删除：根据下标删除
        delPandes(state,{payload}){
            // console.log(1111,payload);
            state.panes.splice(payload,1);
        }
    }
})
export const {addPanes,delPandes} = commonSlice.actions;
export const useSelectorCommon = ()=>useSelector((state:any)=>state.common);
export default commonSlice.reducer;